<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台公用控制器。
 *
 **/

namespace Home\Controller;

use Think\Controller;

class ComController extends Controller
{

    public function _initialize()
    {
//        C(setting());

        $links = M('links')->limit(10)->order('o ASC')->select();
        $this->assign('links',$links);

    }

    protected function searchBy( $keywords, $p = 1 ){

        if(!empty($keywords)){
            $p = intval($p) > 0 ? $p : 1;
            $pagesize = 10;#每页数量
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $articleDB = M('article as a');
            $orderby = "a.aid DESC";
            $map['a.sid'] = CJWT;
            $map['a.title'] = array('like',"%".$keywords."%");
            $field = "a.aid,a.title,a.t";
            $count = $articleDB->where($map)->count();
            $list = $articleDB
                ->field($field)
                ->where($map)
                ->order($orderby)
                ->limit($offset . ',' . $pagesize)
                ->select();
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $list[$key]['title'] = str_replace($keywords, "<span style='color: red'>" . $keywords . "</span>", $value['title']);
                }
            }else{
                return ["code"=>400, "msg"=>"搜索结果为空"];
            }
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            return ["list"=>$list,"page"=>$page];
        }else{
            return ["code"=>400, "msg"=>"关键词为空"];
        }
    }
}